Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
npm install --save rax-types
Add node_modules/rax-types
to your tsconifg.json file, like so:
{
"compilerOptions": {
"typeRoots": [
"node_modules/rax-types",
"node_modules/@types"
],
"paths": {
"rax": ["node_modules/rax-types"]
}
}
}
import * as Rax from 'rax';
const createElement = Rax.createElement;
// FunctionComponent
interface PersonProps {
name: string;
age: number;
}
const Person: Rax.FunctionComponent<PersonProps> = (props) => {
return (
<text>
hello! I'm {props.name} and I'm {props.age} years old!
</text>
);
};
// FunctionComponent With Ref
export interface FancyButtonProps {
onClick: () => void;
children?: Rax.RaxNode;
}
export interface FancyButton {
getClickCount(): number;
}
export const FancyButton = Rax.forwardRef((props: FancyButtonProps, ref: Rax.Ref<FancyButton>) => {
const [count, setCount] = Rax.useState(0);
Rax.useImperativeHandle(ref, () => ({
getClickCount() {
return count;
}
}));
return (
<div
onClick={() => {
setCount(count + 1);
props.onClick();
}}
>
{props.children}
</div>
);
});
interface AppState {
name: string;
age: number;
}
type AppActions = { type: 'getOlder' } | { type: 'resetAge' };
function reducer(s: AppState, action: AppActions): AppState {
switch (action.type) {
case 'getOlder':
return { ...s, age: s.age + 1 };
case 'resetAge':
return { ...s, age: 0 };
}
}
const initialState = {
name: 'Rax',
age: 18
};
export function App() {
const [state, dispatch] = Rax.useReducer(reducer, initialState);
const birthdayRef = Rax.useRef<FancyButton>(null);
Rax.useLayoutEffect(() => {
if (birthdayRef.current !== null) {
birthdayRef.current.getClickCount();
} else {
// this looks redundant but it ensures the type actually has "null" in it instead of "never"
// $ExpectType null
console.log(birthdayRef.current);
}
});
return (
<Rax.Fragment>
<Person {...state} />
<FancyButton
onClick={() => {
if (birthdayRef.current !== null) {
console.log(birthdayRef.current.getClickCount());
}
dispatch({ type: 'getOlder' });
}}
ref={birthdayRef}
>
Birthday time!
</FancyButton>
<FancyButton onClick={() => dispatch({ type: 'resetAge' })}>Let's start over.</FancyButton>
</Rax.Fragment>
);
}
export default App;
FAQs
TypeScript definitions for Rax
We found that rax-types demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.